package cn.itsource.system.service.impl;



import cn.itsource.org.domain.User;
import cn.itsource.system.domain.Permission;
import cn.itsource.system.mapper.PermissionMapper;
import cn.itsource.system.query.PermissionQuery;
import cn.itsource.system.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Transactional

    public void save(Permission permission) {
        if(permission.getId() == null){
            permissionMapper.insert(permission);
        }else {
            permissionMapper.updateByPrimaryKeySelective(permission);
        }
    }

    @Override
    @Transactional

    public int deleteByPrimaryKey(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Permission> pageInfo(PermissionQuery permissionQuery) {
        PageHelper.startPage(permissionQuery.getCurrentPage(),permissionQuery.getPageSize());
        List<Permission> permissions = permissionMapper.selectAllByQuery(permissionQuery);
        return new PageInfo<Permission>(permissions);
    }

    @Override
    @Transactional
    public void deleteByCollection(List<Permission> permissions) {
        permissionMapper.deleteByConllection(permissions);
    }

    @Override
    public Boolean exist(Permission permission) {
        //根据用户名去数据库查询用户对象
        Permission permissionByDB = permissionMapper.getByName(permission.getName());
        if (permission.getId() == null) {
            return permissionByDB == null;
        }else{
            //根据用户名去数据库查询
            if (permissionByDB == null) {
                return true;
            }else{
                return permissionByDB.getId().equals(permission.getId());
            }
            //值为0： OK的
            //值不为0： 1个       拿到这个对象的值，和前端的id进行匹配，
            // 如果相等ok，如果不相等，就不ok
        }
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.seletAll();
    }


}
